package com.groupfour.attendancemanagementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfour.attendancemanagementsystem.dao.ClassesDao;
import com.groupfour.attendancemanagementsystem.entity.DTO.CRUClassesDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.*;
import com.groupfour.attendancemanagementsystem.service.ClassesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@Transactional
@Service
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesDao classesDao;

    /**
     * @author 卑微小峰
     * @return List<RDepartmentDTO>
     * @describe 查询所有院系
     */
    @Override
    public List<RDepartmentVO> selectAllDepartment() {
        return classesDao.selectAllDepartment();
    }

    /**
     * @author 卑微小峰
     * @param departmentCode
     * @return List<RMajorDTO>
     * @describe 查询所有专业（全校或某一院系）
     */
    @Override
    public List<RMajorVO> selectAllMajor(String departmentCode) {
        return classesDao.selectAllMajor(departmentCode);
    }

    @Override
    public List<RTeacherVO> selectAllTeacherNameByEmploymentStatus() {
        return classesDao.selectAllTeacherNameByEmploymentStatus();
    }

    @Override
    public int addClasses(CRUClassesDTO cruClassesDTO) {
        // 生成classesCode
        String classesCode = "1990006";

        return classesDao.addClasses(cruClassesDTO,classesCode);
    }

    @Override
    public int updateClasses(CRUClassesDTO cruClassesDTO, String classesCode) {
        // 获取修改时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time= df.format(new Date());
        Timestamp updateTime= Timestamp.valueOf(time);
        return classesDao.updateClasses(cruClassesDTO, classesCode, updateTime);
    }

    @Override
    public Map<String, Object> selectClasses(CRUClassesDTO cruClassesDTO, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();

        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);

        List<RClassesVO> classesVOList = classesDao.selectClasses(cruClassesDTO);

        //创建分页信息对象
        PageInfo<RClassesVO> pageInfo = new PageInfo<>(classesVOList);
        //将结果返回
        List<RClassesVO> result = pageInfo.getList();

        List<RClassesVO> resultVO = new ArrayList<>();
        for (RClassesVO rClassesVO : result) {
            RClassesVO vo = new RClassesVO();
            BeanUtils.copyProperties(rClassesVO,vo);
            resultVO.add(vo);
        }
        map.put("pageCount",pageInfo.getTotal());
        map.put("result",resultVO);

        return map;
    }

    @Override
    public RClassesVO selectClassesByClassesCode(String classesCode) {
        return classesDao.selectClassesByClassesCode(classesCode);
    }


}
